//
//  SearchV1Cell.m
//  danpin
//
//  Created by chuangjia on 28/6/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "SearchV1Cell.h"
#import "DWTagList.h"
@interface SearchV1Cell()<DWTagListDelegate>

@property (nonatomic, strong) DWTagList *tagList;
@end
@implementation SearchV1Cell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _tagList = [[DWTagList alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, 50.0f)];
        _tagList.backgroundColor =[UIColor clearColor];
        [_tagList setAutomaticResize:YES];
        [_tagList setTagDelegate:self];
        
        [self.contentView addSubview:_tagList];
        
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.key =nil;
    self.tagList.tagDelegate =nil;
    self.tagList=nil;
//    DLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.data =model;
    self.key =key;
    self.indexPath=indexPath;
    NSArray *tagAr =(NSArray *)model;
     NSMutableArray *array =[NSMutableArray array];
    for (id key in tagAr) {
        if (key && [key isKindOfClass:[NSDictionary class]]) {
            NSString *name =[key objectForKey:@"name"];
            [array addObject:name];
        }else if (key && [key isKindOfClass:[NSString class]])
        {
            [array addObject:key];
        }
    }
    if ([key isEqualToString:@"history"]) {
        // Customisation
        [_tagList setCornerRadius:4.0f];
        [_tagList setBorderColor:UIColorFromRGB(0x999999)];
        [_tagList setBorderWidth:0.5f];
        [_tagList setHighlightedBackgroundColor:UIColorFromRGB(0xf4f4f4)];
    }else if ([key isEqualToString:@"hot"])
    {
        self.tagList.textColor =[UIColor whiteColor];
        [_tagList setCornerRadius:4.0f];
        [_tagList setBorderColor:UIColorFromRGB(0x999999)];
        [_tagList setBorderWidth:0.5f];
        self.tagList.textShadowColor =nil;
        self.tagList.textShadowOffset =CGSizeZero;
        [self.tagList setBorderColor:UIColorFromRGB(0x285790)];
        [self.tagList setTagBackgroundColor:UIColorFromRGB(0x285790)];
        [_tagList setHighlightedBackgroundColor:UIColorFromRGB(0x256FB2)];
    }
    [self.tagList setTags:array];
    
    [self layoutSubviews];
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSArray *tagAr =(NSArray *)model;
    DWTagList *ttl= [[DWTagList alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, 0.0f)];
    [ttl setAutomaticResize:YES];
    NSMutableArray *array =[NSMutableArray array];
    for (id key in tagAr) {
        if (key && [key isKindOfClass:[NSDictionary class]]) {
            NSString *name =[key objectForKey:@"name"];
            [array addObject:name];
        }else if (key && [key isKindOfClass:[NSString class]])
        {
            [array addObject:key];
        }
    }
    [ttl setTags:array];
    CGSize size =[ttl fittedSize];
    ttl=nil;
    return size.height+PHOTO_FRAME_WIDTH;
}

-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
- (void)selectedTag:(NSString *)tagName tagIndex:(NSInteger)tagIndex
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_tag,self,tagName,self.key,self.indexPath);
    }
}
@end
